package com.dudsbury.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Font;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JCheckBox;


import com.dudsbury.database.DatabaseExtension;
import com.dudsbury.database.Searchable;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.BevelBorder;
import javax.swing.SwingConstants;

public class SearchScreen extends JFrame {

	SearchScreen thisFrame = this;
	JCheckBox chkBook, chkItem, chkCus;
	private ArrayList<Searchable> bookingList, customerList, itemList;
	private JPanel contentPane;
	private JTextField searchText;
	DefaultListModel<Searchable> model;
	JList<Searchable> list;
	
	
	public SearchScreen() {
		setTitle("Search");
		setVisible(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 615, 443);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		
		
		model = new DefaultListModel<Searchable>();
		loadSearchables();
		
		JPanel panel = new JPanel();
		panel.setBounds(15, 16, 568, 59);
		panel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));

		JPanel panel_1 = new JPanel();
		panel_1.setBounds(15, 86, 568, 69);
		panel_1.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));

		JButton btnEditSelected = new JButton("Edit Selected");
		btnEditSelected.setBounds(149, 367, 117, 23);
		btnEditSelected.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				 list.getSelectedValue().onSelected(thisFrame);
			}
		});

		JButton btnExit = new JButton("Exit");
		btnExit.setBounds(490, 367, 93, 23);
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				thisFrame.dispose();
			}
		});
		
		JButton btnClearSearchResults = new JButton("Clear Search Results");
		btnClearSearchResults.setBounds(299, 367, 163, 23);
		btnClearSearchResults.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				model.clear();
			}
		});
		
		JButton btnViewReport = new JButton("View Report");
		btnViewReport.setBounds(15, 367, 106, 23);
		btnViewReport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				 new ReportScreen(list.getSelectedValue());
				 thisFrame.dispose();
			}
		});
		
		JLabel lblSearch = new JLabel("Search");
		lblSearch.setHorizontalAlignment(SwingConstants.CENTER);
		lblSearch.setFont(new Font("Arial", Font.BOLD, 18));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblSearch, GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblSearch, GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
					.addContainerGap())
		);
		panel.setLayout(gl_panel);

		JLabel lblPleaseEnterSearch = new JLabel("Please Enter Search Term:");
		lblPleaseEnterSearch.setBounds(12, 12, 209, 14);

		searchText = new JTextField();
		searchText.setBounds(171, 9, 262, 20);
		searchText.setColumns(30);

		JButton btnSearch = new JButton("Search");
		btnSearch.setBounds(456, 8, 79, 23);
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				search();
			}
		});

		chkBook = new JCheckBox("Bookings");
		chkBook.setBounds(171, 33, 83, 23);

		chkCus = new JCheckBox("Customers");
		chkCus.setBounds(256, 33, 93, 23);

		chkItem = new JCheckBox("Items");
		chkItem.setBounds(351, 33, 79, 23);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBounds(15, 166, 568, 190);
		panel_2.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		panel_1.setLayout(null);
		panel_1.add(lblPleaseEnterSearch);
		panel_1.add(chkBook);
		panel_1.add(chkCus);
		panel_1.add(chkItem);
		panel_1.add(searchText);
		panel_1.add(btnSearch);
		
				JScrollPane scrollPane = new JScrollPane();
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 544, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_panel_2.setVerticalGroup(
			gl_panel_2.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 163, GroupLayout.PREFERRED_SIZE)
					.addGap(134))
		);
		
				 list = new JList();
				 scrollPane.setViewportView(list);
				 list.setModel(model);
		panel_2.setLayout(gl_panel_2);
		contentPane.setLayout(null);
		contentPane.add(panel);
		contentPane.add(panel_1);
		contentPane.add(btnClearSearchResults);
		contentPane.add(btnExit);
		contentPane.add(btnViewReport);
		contentPane.add(btnEditSelected);
		contentPane.add(panel_2);
	}

	public void search() {
		
		model.clear();
		loadSearchables();
		System.out.println("Searching: |" + searchText.getText() + "|");
		
		if (searchText.getText().length()<2) {
			if (chkBook.isSelected()) {
			for (Searchable s : bookingList)
				model.addElement(s);
			}
			if (chkCus.isSelected()) {
			for (Searchable s : customerList)
				model.addElement(s);
			}
			if (chkItem.isSelected()) {
			for (Searchable s :itemList)
				model.addElement(s);
			}
			return;
		}
		String[] query = searchText.getText().split(" |\\.|\\-");
		if (chkBook.isSelected()) {
			for (Searchable s : bookingList) {
				if (s.search(query)) {
					model.addElement(s);
					System.out.println("Adding Booking to model");
				}
			}

		}
		
		if (chkCus.isSelected()) {
			for (Searchable s : customerList) {
				if (s.search(query)) {
					System.out.println("Adding Customer to model");
					model.addElement(s);
				}
			}

		}
		
		if (chkItem.isSelected()) {
			for (Searchable s : itemList) {
				if (s.search(query)) {
					System.out.println("Adding Item to model");
					model.addElement(s);
				}
			}

		}
	}

	public void loadSearchables() {

		bookingList = new ArrayList<Searchable>();
		customerList = new ArrayList<Searchable>();
		itemList = new ArrayList<Searchable>();
		bookingList.addAll(DatabaseExtension.getBookings());
		for (Searchable s : DatabaseExtension.getCustomers()) {
			customerList.add(s);
			System.out.println(s);
		}
		for (Searchable s : DatabaseExtension.getItems()) {
			itemList.add(s);
			
		}

		//System.out.printf("Bookings in list:%d items:%d customers:%d\n", bookingList.size(),itemList.size(),customerList.size());
	}
}
